<?php
	require_once('includes.php');
	Session::start();
	
	//This function is used to check if the user is logged 
	//Only used index.php and other sites which both registered and unregistered users can view
	
	//Check if session exists
	if (isset($_SESSION['session'])) {
		//Unserialize the object in session
		$session = unserialize($_SESSION['session']);
		$user_id = $session->getUserID();
		$user_role = User::findRole($user_id);
		//Check for roles
		if ($user_role === 'patient') {
			$user = Patient::createUser($user_id);
						
		} else if ($user_role === 'doctor') {
			$user = Doctor::createUser($user_id);
		} else {
			$user = User::createUser($user_id); //For admin
		}
	}

	//Allow access for only validated user
	function onlyValid() {
		$session = unserialize($_SESSION['session']);
		if (!User::checkValid(intval($session->getUserID()))) {
			header('Location: ' . DIR . '/unauthorized.php');
		}
	}
	//Allow access for only patient
	function onlyPatient() {
		$session = unserialize($_SESSION['session']);
		if (User::findRole($session->getUserID()) === 'doctor') {
			header('Location: ' . DIR . '/unauthorized.php');
		}
	}
	//Allow access for only doctor
	function onlyDoctor() {
		$session = unserialize($_SESSION['session']);
		if (User::findRole($session->getUserID()) === 'patient') {
		header('Location: ' . DIR . '/unauthorized.php');
		}
	}
	//Allow access for only admin
	function onlyAdmin() {
		$session = unserialize($_SESSION['session']);
		if (User::findRole($session->getUserID()) !== 'admin') {
			header('Location: ' . DIR . '/unauthorized.php');
		}
	}
	

?>